<template>
  <div class="page-modal">
    <el-dialog
      :title="modalName"
      v-model="dialogVisible"
      width="30%"
      center
      destroy-on-close
    >
      <my-form v-bind="modalConfig" v-model="formData"></my-form>
      <slot></slot>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleConfirmClick">
            确 定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from 'vue'
import { useStore } from 'vuex'

import myForm from '@/base-ui/form'
import { record } from '@/service/main/system/system'

export default defineComponent({
  components: {
    myForm
  },
  props: {
    // 用来存放form表单种类及其表单属性→给ypf-form组件绑定
    modalConfig: {
      type: Object,
      required: true
    },
    // 用来接收新增 or 编辑传递过来的对象
    defaultInfo: {
      type: Object,
      default: () => ({})
    },
    //组件的名字
    pageName: {
      type: String,
      require: true
    },
    // formdata中其他额外的参数
    otherInfo: {
      type: Object,
      default: () => ({})
    },
    modalName: {
      type: String,
      require: true
    }
  },
  setup(props) {
    const dialogVisible = ref(false)
    const formData = ref<any>({})

    //监听defaultInfo的变化
    watch(
      () => props.defaultInfo,
      (newValue) => {
        //把传入的表单的名都动态的进行赋值
        for (const item of props.modalConfig.formItems) {
          formData.value[`${item.field}`] = newValue[`${item.field}`]
        }
      }
    )

    // 点击确定按钮的逻辑
    const store = useStore()
    const handleConfirmClick = async () => {
      dialogVisible.value = false

      if (Object.keys(props.defaultInfo).length) {
        // 编辑
        console.log('编辑用户')
        console.log(props.defaultInfo)
        console.log(formData)
        let id = ''
        if (props.defaultInfo.leaveId) {
          id = props.defaultInfo.leaveId
        } else if (props.defaultInfo.athCheckId) {
          id = props.defaultInfo.athCheckId
        } else if (props.defaultInfo.makeTimeId) {
          id = props.defaultInfo.makeTimeId
          formData.value.type = 2
        } else if (props.defaultInfo.learningId) {
          id = props.defaultInfo.learningId
          formData.value.type = 2
        } else {
          id = props.defaultInfo.id
        }
        if (formData.value?.Usertype && formData.value?.Usertype == 2) {
          const { status } = await record({
            userId: props.defaultInfo.id,
            yearStartTime: formData.value.year_start_time,
            yearEndTime: formData.value.year_end_time,
            restRecord: formData.value.rest_record,
            refillCard: formData.value.refill_card
          })
          if (status == 200) {
            store.dispatch('system/getPageListAction', {
              pageName: 'member',
              queryInfo: {
                pageNum: 1,
                pageSize: 10
              }
            })
          }
        } else {
          store.dispatch('system/editPageDataAction', {
            pageName: props.pageName,
            editData: {
              ...formData.value,
              ...props.otherInfo.value
            },

            id: id
          })
        }
      } else {
        // 新建
        console.log('新建用户 +111')

        store.dispatch('system/createPageDataAction', {
          pageName: props.pageName,
          newData: { ...formData.value, ...props.otherInfo.value }
        })
      }
    }

    return {
      dialogVisible,
      formData,
      handleConfirmClick
    }
  }
})
</script>

<style scoped></style>
